package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.bean.Result;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Hospital;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/8/25 23:27
 */
@Api(tags = {"医院信息"})
@RestController
@RequestMapping("/api/hosp")
public class ApiHospitalController {

    @Autowired
    private HospitalService hospitalService;

    @PostMapping("/hospital/show")
    public Result<Hospital> getHospitalInfo(HttpServletRequest request) {
        // 获取参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(parameterMap);
        // 获取编号
        String hoscode = (String)stringObjectMap.get("hoscode");
        // 根据编号查询
        Hospital hospital = hospitalService.getHospitalInfoByHoscode(hoscode);
        // 进行验证
        // 获取signkey
       /* String requestSignKey = (String)stringObjectMap.get("sign");
        // 根据hoscode获取预约平台的医院设置信息的密钥
        String signKey = hospitalService.getSignKeyWithHoscode(hoscode);
        String encrypt = MD5.encrypt(signKey);
        if(!StringUtils.isEmpty(requestSignKey) && !StringUtils.isEmpty(signKey) && encrypt.equals(requestSignKey)) {
            return Result.ok(hospital);
        }else {
            throw new YyghException(20001, "密钥不正确，无法正确获取信息");
        }*/
        return Result.ok(hospital);
    }

    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String,Object> resultMap = HttpRequestHelper.switchMap(parameterMap);
        // 进行校对
        String requestSignKey = (String)resultMap.get("sign");
        String requestHoscode = (String)resultMap.get("hoscode");
        String signKey = hospitalService.getSignKeyWithHoscode(requestHoscode);
        // 因为请求的signkey是加密过后的，所以我们也要加密进行比较
        String encrypt = MD5.encrypt(signKey);
        if(!StringUtils.isEmpty(requestSignKey) && !StringUtils.isEmpty(signKey) && encrypt.equals(requestSignKey)) {
            // 获取图标信息
            String logoData = (String)resultMap.get("logoData");
            // base64在传输过程中 + -> " ",我们需要进行还原
            String result = logoData.replaceAll(" ", "+");
            resultMap.put("logoData", result);
            // 调用service层保存医院信息到mongodb中
            hospitalService.saveHospital(resultMap);
            return Result.ok();
        }else {
            throw new YyghException(20001, "保存失败");
        }
    }
}
